<template>
	<view>
		<search></search>
		<view class="scroll_container">
			<scroll-view class="left" scroll-y :style="{height:wh + 'px'}" >
				<view
					class="left_item" 
					:class="{active:item.active}" 
					v-for="(item,index) in cateList" 
					:key="index"
					@click="handleCate(index)"
				>{{item.cat_name}}</view>
			</scroll-view>
			<scroll-view class="right" scroll-y :style="{height:wh + 'px'}"  :scroll-top="scrollTop">
				<view class="" v-for="(val,i) in cateList2" :key="i">
					<text class="cate-lv2-title">/{{val.cat_name}}/</text>
					<view class="cate-lv2-box">
						<view class="cate-lv2-box_item" v-for="(item,index) in val.children" :key="index" @click="goGoodsList(item)">
							<image class="title_img" :src="item.cat_icon" mode=""></image>
							<text>{{item.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMixin from '../../mixins/tabbar-badge.js'
	export default {
		data() {
			return {
				wh: 0,
				cateList: [],
				cateList2: [],
				scrollTop: 0
			};
		},
		mixins:[badgeMixin],
		onLoad() {
			// 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
			// 获取当前系统的信息
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 56
			this.getCateList()
		},
		methods:{
			async getCateList() {
				const {data} = await uni.$http.get('/api/public/v1/categories')
				if (data.meta.status !== 200) return uni.$showMsg()
				data.message.map(val => {
					this.$set(val,'active',false)
				})
				data.message[0].active = true
				this.cateList = data.message
				this.cateList2 = data.message[0].children
				console.log(this.cateList)
			},
			handleCate(val) {
				this.cateList.map(item => {
					item.active = false
				})
				this.cateList[val].active = true
				this.cateList2 = this.cateList[val].children
				this.scrollTop = this.scrollTop ? 0 : 1
			},
			goGoodsList(val) {
				uni.navigateTo({
					url:"../../subpkg/goods_list/goods_list?cid=" + val.cat_id
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll_container {
	display: flex;
	.left {
		width: 120px;
		.left_item {
			line-height: 60px;
		    background-color: #f7f7f7;
		    text-align: center;
		    font-size: 12px;
		}
		.active {
			position: relative;
			background-color: #fff;
			color: #c00000;
			&::before {
				content: '';
				position: absolute;
				top: 15px;
				left: 0;
				display: inline-block;
				width: 2px;
				height: 30px;
				background-color: #c00000;
			}
		}
	}
	.right {
		.cate-lv2-title {
			display: block;
			font-size: 12px;
		    font-weight: bold;
		    text-align: center;
		    padding: 15px 0;
		}
		.title_img {
			width: 60px;
			height: 60px;
		}
		.cate-lv2-box {
			padding: 0 15px;
			display: flex;
			flex-wrap: wrap;
			.cate-lv2-box_item {
				width: 33.33%;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-bottom: 10px;
			}
		}
	}
}
</style>
